/*
 * GccApplication1.c
 *
 * Created: 26/12/2013 16.58.31
 *  Author: cicci_000
 */ 


#include <avr/io.h>
#include <avr/interrupt.h>
#include "sr_input.h"
#include "sr_output.h"
#include "Display.h"
#include "digital_io.h"
#include "board_init.h"
#include "wait.h"
#include "SR_struct.h"   //test
#include "timer.h"

//visualstate include files
#include "PMAR.h"
#include "PMARAction.h"
#include "PMARData.h"
#include "PMARSEMBDef.h"
#include "PMARSEMLibB.h"
#include "PMARRealLink.h"

#include "simpleEventHandler.h"
#include "InputToVS.h"


//#include "SEMBDef.h"
//#include "SEMTypes.h"
//#include "EventHandler.h"
//#include "SimpleEventHandler.h"


unsigned char Digital_Input[MAX_BYTE_INPUT_IO];
unsigned char Digital_Output[MAX_BYTE_OUTPUT_IO];
unsigned char Digital_Output_Temp[MAX_BYTE_OUTPUT_IO_TEMP];
unsigned char InputData[32];

SR_Digital_Input_type SR_Digital_Input_TEST[5];

//Visualstate declaration
unsigned char cc;
/* Define action expression variable. */
SEM_ACTION_EXPRESSION_TYPE actionExpressNo;
/* Define and initialize. In this case the reset event is SE_RESET. */
SEM_EVENT_TYPE eventNo = SE_RESET;

void HandleError(unsigned char ErrNum) 
{
	ErrNum++;
}

int main(void)
{   
	unsigned char volatile EmerPB=0;
	unsigned char i; 
	unsigned char out_index=15;  
	cli();
	BrdRegisterInit();
	SR_Input_Init();
	DI_Init();
	SR_Output_Init();
	DO_Init();
	
	/* init Basic API */
	//SEM_InitAll();
	/* init RealLink */
	//VS_RealLinkInit();
	SEQ_Initialize();
	PMARSEM_Init();
	VS_RealLinkInit();
	sei();
//	if ((cc = DEQ_Initialize()) != UCC_OK)
//	HandleError(cc);	
	timer_init();
//	timer_set (0, 100);
	
/*
	while(1){
		if (timer_expired (0)) {
				DO_Set(out_index);
				SR_Output_Write();	
				timer_set (0, 100);	
				out_index++;
		}
		
	}
*/
	
    while(1)
    {
        
		//************************************************************************
		//							TEST DISPLAYS
		// TODO: VERIFICARE IL MOTIVO PER CUI ALCUNI SEGMENTI SONO PARZIALMENTE ACCESI
		//************************************************************************
		//TODO:: Please write your application code 
		/*
		BigDisplayWriteValue(2345);
        SmallDisplayWriteValue(321);
		
		DisplayWriteValues();
		*/
		//PORTC|=0x1F;
		
		//************************************************************************
		//							TEST INPUT		
		// TODO: Creare la funzione per la gestione del pulsante di emergenza (PE5)
		//************************************************************************
		
		
		SR_Input_Mean();
      	for (i=0;i<32;i++) {
		   	 InputData[i]=DI_Test(i);
	    }
		Keypad_scan(InputData);
		
		EmerPB =(PINE >> 5)&0x01;
		i++;
		
		Input_scan (InputData);
		
		Emer_Button();

		//************************************************************************
		//							TEST OUTPUT		
		// TODO:
		//************************************************************************
		/*
		DO_Init();
		Wait(1000);
		
		SR_Output_Write();
		DO_Set(0);
		SR_Output_Write();
		DO_Reset(0);
		SR_Output_Write();
		DO_Set(1);
		SR_Output_Write();
		DO_Reset(1);
		SR_Output_Write();
		DO_Set(2);
		SR_Output_Write();
		DO_Reset(2);
		SR_Output_Write();	
		
		for(i=0;i<22;i++) {
			DO_Set(i);
			SR_Output_Write();	
			Wait(1000);		
		}	
		
		*/	
		
		//************************************************************************
		//							TEST Visualstate		
		// TODO: 
		//************************************************************************	
		/* Deduct the event. */
		/* Deduct the event. */
		VS_WAIT(SEMPMAR);  // visualstate Reallink support
		
		if ((cc = PMARSEM_Deduct(eventNo)) != SES_OKAY)
		HandleError(cc);
		/* Get resulting action expressions and execute them. */
		while ((cc = PMARSEM_GetOutput(&actionExpressNo)) == SES_FOUND)
		PMARSEM_Action(actionExpressNo);
		if (cc != SES_OKAY)
		HandleError(cc);
		/* Change the next state vector. */
		if ((cc = PMARSEM_NextState()) != SES_OKAY)
		HandleError(cc);
		/* Get next event from queue */
		while (SEQ_RetrieveEvent(&eventNo) == UCC_QUEUE_EMPTY)
		{
		Wait(1);
	    SR_Input_Mean();
      	for (i=0;i<32;i++) {
		   	 InputData[i]=DI_Test(i);
	    }
		Keypad_scan(InputData);
        Input_scan (InputData);
		Wait(1);
		SR_Output_Write();	
		Emer_Button();
		}
		
		
    }
	
	
}